package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BookDAO;
import dao.ReviewDAO;
import pojo.Book;
import pojo.Discuss;
import pojo.Review;

@WebServlet("/review")
public class ReviewListServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=UTF-8");

        //截取排序方式
        String sort;
        if (request.getParameter("sort") == null){
            sort = "default";
        } else {
            sort = request.getParameter("sort");
        }
        request.setAttribute("sort", sort);
        
        //截取类别
        String category;
        if (request.getParameter("category") == null){
            category = "all";
        } else {
            category = request.getParameter("category");
        }
        request.setAttribute("reviewCategory", category);

        //截取书码
        int bk_id = 0;
        try {
            bk_id = Integer.parseInt(request.getParameter("bk_id"));
        } catch (NumberFormatException e) {
            // 当浏览器没有传参数start时
            if (request.getAttribute("bk_id") == null){
                bk_id = 404;
            }
        }
        request.setAttribute("bk_id", bk_id);

        //截取页码
        int start;
        int count = 5;
        try {
            start = Integer.parseInt(request.getParameter("start"));
        } catch (NumberFormatException e) {
            // 当浏览器没有传参数start时
            start = 0;
        }
        
        //计数
        int next = start + count;
        int pre = start - count;

        int total = new ReviewDAO().getTotal(bk_id);

        int last;
        if (0 == total % count)
            last = total - count;
        else
            last = total - total % count;

        pre = pre < 0 ? 0 : pre;
        next = next > last ? last : next;

        request.setAttribute("next", next);
        request.setAttribute("pre", pre);
        request.setAttribute("last", last);

        List<Review> reviews = null;
        if ("all".equals(category)) {
            if ("default".equals(sort)) {
                reviews = new ReviewDAO().list(start, count, bk_id);
            } else {
                reviews = new ReviewDAO().listBySort(start, count, bk_id, sort);
            }
        } else {
            if ("default".equals(sort)) {
                reviews = new ReviewDAO().listByCategory(start, count, bk_id, category);
            } else {
                reviews = new ReviewDAO().list(start, count, bk_id, category, sort);
            }
        }
        request.setAttribute("reviews", reviews);

        request.getRequestDispatcher("listReview.jsp").forward(request, response);
    }
}